/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_str_to_utf8.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/11 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../../src/module/oracle/config.h"
#include "../../test_ora_comm.h"
#include <oracle/comm/ora_charset.h>
#include <apmtest_comm.h>
#include <oracle/comm/ora_comm.h>

extern void __ora_destroy_session(L7session_ora **pp_st_session);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正确测试
 * 871 -> UTF8
 */
static void __case1(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    char *p_bin = (char *) "73656c65637420646570746e6f2066726f6d2064657074";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    char *p_sql = NULL;
    unsigned int ui_sql_utf8_len = 0;

    CU_ASSERT_EQUAL(ora_str_to_utf8(p_st_ora_sess, 871, arr_payload, ui_payload_len, &p_sql, &ui_sql_utf8_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_sql, "select deptno from dept");
    CU_ASSERT_EQUAL(ui_sql_utf8_len, strlen("select deptno from dept"));

    ora_free((void **) &p_sql);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 正确测试
 * 873 -> UTF8
 */
static void __case2(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    char *p_bin = (char *) "73656c65637420745f626c6f622c745f636c6f622066726f6d206d796461746174797065";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    char *p_sql = NULL;
    unsigned int ui_sql_utf8_len = 0;

    CU_ASSERT_EQUAL(ora_str_to_utf8(p_st_ora_sess, 873, arr_payload, ui_payload_len, &p_sql, &ui_sql_utf8_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_sql, "select t_blob,t_clob from mydatatype");
    CU_ASSERT_EQUAL(ui_sql_utf8_len, strlen("select t_blob,t_clob from mydatatype"));

    ora_free((void **) &p_sql);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 正确测试
 * 852 -> GBK
 */
static void __case3(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    char *p_bin = (char *) "53454c45435420555345522046524f4d204455414c00";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    char *p_sql = NULL;
    unsigned int ui_sql_utf8_len = 0;

    CU_ASSERT_EQUAL(ora_str_to_utf8(p_st_ora_sess, 852, arr_payload, ui_payload_len, &p_sql, &ui_sql_utf8_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_sql, "SELECT USER FROM DUAL\0");
    CU_ASSERT_EQUAL(ui_sql_utf8_len, strlen("SELECT USER FROM DUAL") + 1);

    ora_free((void **) &p_sql);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 正确测试
 * 178 -> WINDOWS-1252
 */
static void __case4(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    char *p_bin = (char *) "424547494e2044424d535f4f55545055542e44495341424c453b20454e443b";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    char *p_sql = NULL;
    unsigned int ui_sql_utf8_len = 0;

    CU_ASSERT_EQUAL(ora_str_to_utf8(p_st_ora_sess, 178, arr_payload, ui_payload_len, &p_sql, &ui_sql_utf8_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_sql, "BEGIN DBMS_OUTPUT.DISABLE; END;");
    CU_ASSERT_EQUAL(ui_sql_utf8_len, strlen("BEGIN DBMS_OUTPUT.DISABLE; END;"));

    ora_free((void **) &p_sql);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 异常测试
 * 0
 */
static void __case5(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    char *p_bin = (char *) "73656c65637420745f6e756d6265722c745f666c6f61742c312066726f6d206d796461746174797065";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    char *p_sql = NULL;
    unsigned int ui_sql_utf8_len = 0;

    CU_ASSERT_EQUAL(ora_str_to_utf8(p_st_ora_sess, 0, arr_payload, ui_payload_len, &p_sql, &ui_sql_utf8_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_sql, "select t_number,t_float,1 from mydatatype");
    CU_ASSERT_EQUAL(ui_sql_utf8_len, strlen("select t_number,t_float,1 from mydatatype"));

    ora_free((void **) &p_sql);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

int add_suite_ora_str_to_utf8(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ora_str_to_utf8()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
